package com.tyqx.hashchainbiz.controller.jzt;

import com.tyqx.hashchainbean.module.JZTCodeCirculationRelationship;
import com.tyqx.hashchainbean.module.JZTLogisticsRelationship;
import com.tyqx.hashchainbiz.service.JZTCodeCirculationRelationshipService;
import com.tyqx.hashchainbiz.service.impl.ExcelService;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import hashchainutil.common.EmptyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/JZTApi/codeCirculation")
@CrossOrigin
public class JZTCodeCirculationRelationshipController {

    @Resource
    private ExcelService excelService;

    @Resource
    private JZTCodeCirculationRelationshipService jztCodeCirculationRelationshipService;

    @RequestMapping(value="/addJZTCodeCirculationRelationship", method = RequestMethod.POST)
    @ResponseBody
    public Dto addJZTCodeCirculationRelationship(@RequestBody JZTCodeCirculationRelationship jztCodeCirculationRelationship) throws Exception {

        String msg="";
        Integer flag= jztCodeCirculationRelationshipService.addJZTCodeCirculationRelationship(jztCodeCirculationRelationship);
        if(flag>0){
            msg="添加追溯关系成功";
        }else{
            msg="添加追溯关系失败";
        }
        return DtoUtil.returnSuccess(msg);
    }

    @RequestMapping(value="/addJZTCodeCirculationRelationshipUploadExcel", method = RequestMethod.POST)
    @ResponseBody
    public Dto addJZTCodeCirculationRelationshipUploadExcel(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("fileName");
        if (file.isEmpty()) {
            return DtoUtil.returnFail("文件不能为空","100001");
        }
        String msg="";
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = excelService.getBankListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();
            Integer flag=0;

            Date currentDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime = sdf.parse(sdf.format(currentDate));
            Date updateTime = createTime;

            List<JZTCodeCirculationRelationship> jztCodeCirculationRelationshipList=new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                List<Object> lo = list.get(i);
                JZTCodeCirculationRelationship jztCodeCirculationRelationship=new JZTCodeCirculationRelationship();
                for (int j=0;j<lo.size();j++){
                    if(j==0){
                        jztCodeCirculationRelationship.setParentTraceabilityCode(lo.get(j).toString());
                    }else if(j==1){
                        jztCodeCirculationRelationship.setTraceabilityCode(lo.get(j).toString());
                    }else if(j==2){
                        jztCodeCirculationRelationship.setTraceabilityCodeType(lo.get(j).toString());
                    }
                }
                if(EmptyUtils.isNotEmpty(jztCodeCirculationRelationship.getTraceabilityCode())){

                    jztCodeCirculationRelationship.setCreateTime(createTime);
                    jztCodeCirculationRelationship.setUpdateTime(updateTime);
                    jztCodeCirculationRelationshipList.add(jztCodeCirculationRelationship);
                }
            }
            List<JZTLogisticsRelationship> jztLogisticsRelationshipListError=new ArrayList<>();
            int size=jztCodeCirculationRelationshipList.size();
            if(size>0){
                Integer flag1=jztCodeCirculationRelationshipService.insertBatch(jztCodeCirculationRelationshipList);
                if(flag1>0){
                    msg="添加一码流通关系成功";
                    return DtoUtil.returnSuccess(msg);
                }else{
                    msg="添加一码流通关系失败";
                    return DtoUtil.returnFail(msg, "10001");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            msg="文件解析失败或者添加追溯信息错误";
        }


        return DtoUtil.returnSuccess(msg);
    }


    @RequestMapping(value="/addJZTCodeCirculationRelationshipBatch", method = RequestMethod.POST)
    @ResponseBody
    public Dto addJZTCodeCirculationRelationshipBatch(@RequestBody List<JZTCodeCirculationRelationship> jztCodeCirculationRelationshipList) throws Exception {
        String msg="";
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = sdf.parse(sdf.format(currentDate));
        Date updateTime = createTime;
        for (int i=0;i<jztCodeCirculationRelationshipList.size();i++){

            JZTCodeCirculationRelationship jztCodeCirculationRelationship=jztCodeCirculationRelationshipList.get(i);
            jztCodeCirculationRelationship.setCreateTime(createTime);
            jztCodeCirculationRelationship.setUpdateTime(updateTime);
        }
        Integer flag=jztCodeCirculationRelationshipService.insertBatch(jztCodeCirculationRelationshipList);
        if(flag>0){
            msg="添加一码流通关系成功";
            return DtoUtil.returnSuccess(msg);
        }else{
            msg="添加一码流通关系失败";
            return DtoUtil.returnFail(msg, "10001");
        }
    }
}
